package net.mhelvens.deltaprofiles;

import net.mhelvens.deltaprofiles.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
	@SuppressWarnings("unused") private static final String TAG = MainActivity.class.getSimpleName();
	
	ViewPager            mViewPager;
	FragmentPagerAdapter mSectionsPagerAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// Finding the view(s)
		//
		mViewPager = (ViewPager) findViewById(R.id.pager);
		
		// The action bar
		//
		final ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		
		// The adapter containing the information on the three tab fragments
		//
		mSectionsPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
			public int getCount() { return 3; }
			 
			public Fragment getItem(int position) {
				switch (position) {
				case 0: return new MainRuleListFragment();
				case 1: return new MainPluginListFragment();
				case 2: return new MainLogFragment();
				} return null;
			}
			
			public CharSequence getPageTitle(int position) {
				switch (position) {
				case 0: return getString(R.string.rules  );
				case 1: return getString(R.string.plugins);
				case 2: return getString(R.string.log    );
				} return null;
			}
		};
		
		mViewPager.setAdapter(mSectionsPagerAdapter);
		
		// Creating the tabs
		//
		for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
			actionBar.addTab(actionBar.newTab()
			         .setText(mSectionsPagerAdapter.getPageTitle(i))
			         .setTabListener(this));
		}
		
		// Swipe --> Tab Select
		//
		mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
			public void onPageSelected(int position) {
				actionBar.setSelectedNavigationItem(position);
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	// Tab Click --> Pager View Select
	//
	public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
		mViewPager.setCurrentItem(tab.getPosition());
	}

	public void onTabReselected(Tab tab, FragmentTransaction ft) {
	} // intentionally empty

	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
	} // intentionally empty
}
